package org.lq.his.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
 
import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.lq.his.dao.DictionaryDao;
import org.lq.his.dao.DoctorRankClassDao;
import org.lq.his.dao.PatientDao;
import org.lq.his.dao.RegistrationDao;
import org.lq.his.pojo.Dictionary;
import org.lq.his.pojo.DoctorRankClass;
import org.lq.his.pojo.Patient;
import org.lq.his.pojo.Registration;
import org.lq.his.service.RegistrationService;
import org.lq.his.utils.LqFormatDateUtils;
import org.springframework.stereotype.Service;
@Service("registration")
public class RegistrationServiceImpl implements RegistrationService {

	@Resource
	private RegistrationDao registrationDao;
	
	@Resource
	private DoctorRankClassDao doctorRankClassDao;
	@Resource
	private DictionaryDao dictionaryDao;
	@Resource
	private PatientDao patientDao;
	
	public void setPatientDao(PatientDao patientDao) {
		this.patientDao = patientDao;
	}

	public void setDictionaryDao(DictionaryDao dictionaryDao) {
		this.dictionaryDao = dictionaryDao;
	}

	public void setDoctorRankClassDao(DoctorRankClassDao doctorRankClassDao) {
		this.doctorRankClassDao = doctorRankClassDao;
	}

	public void setRegistrationDao(RegistrationDao registrationDao) {
		this.registrationDao = registrationDao;
	}



	@Override
	public String registration(Registration registration) {
		
		//1.检查该医生挂号是否已达上限。
		DoctorRankClass doctorRankClass = doctorRankClassDao.getByRegistration(registration);
		    //判断现在为上午还是下午,0上午，1下午
			GregorianCalendar ca = new GregorianCalendar();
			Integer shangxiawu = ca.get(GregorianCalendar.AM_PM);
			if(shangxiawu==0) {
				if(doctorRankClass.getAmRegistrationNumber()>=doctorRankClass.getAmLimitNumber()) {
					return "该医生上午挂号已满。";
				}
			}else {
				if(doctorRankClass.getPmRegistrationNumber()>=doctorRankClass.getPmLimitNumber()) {
					return "该医生下午挂号已满。";
				}
			}
		//2.查询该病人有人已经挂号
		registration.setStage(shangxiawu);
		//获取当前日期并格式化
		LqFormatDateUtils lqFormatDateUtils = new LqFormatDateUtils();
		Date d = lqFormatDateUtils.getTodayDate();
		registration.setRegistrationTime(d);
		Registration yesNo = registrationDao.getYesNo(registration);
		if(yesNo!=null&& yesNo.getId()>0&&yesNo.getRegisterState()==0) {
			return "该病人已经挂过该医生的号，可前去登记就诊~";
		}
		//3.如果为余额支付，减少病人相应余额。
			//判断是否为余额支付
			Dictionary zhifu = dictionaryDao.getByIdAndValue(registration.getPaymentMethod().getId(),"余额");
			//如果不为null 的话说明是余额支付
			if(zhifu!=null) {
				Patient patient = patientDao.getById(registration.getPatient().getId());
				//如果余额不足，直接return
				if(patient.getMoney()<registration.getRegistrationMoney()) {
					return "余额不足，请充值~";
				}else {
					//减少余额
					
					patientDao.editPetientMoney(registration.getPatient().getId(), registration.getRegistrationMoney());
				}
			}
		//4.如果没有达到上限，可以挂号. 
		//修改医生排班表的 的挂号数量和上限数量。
		doctorRankClassDao.editLimitAndRegistration(doctorRankClass, shangxiawu);
		//5.添加挂号表记录
		registrationDao.add(registration);
		return "挂号成功";
	}

	@Override
	public List<Registration> getByToday(@Param("start") int start,@Param("limit") int limit,@Param("condition") Registration condition,@Param("column") String column,@Param("orderBy") String orderBy) {
		
		LqFormatDateUtils lqFormatDateUtils = new LqFormatDateUtils();
		Date d = lqFormatDateUtils.getTodayDate();
		condition.setRegistrationTime(d);
		return registrationDao.getByToday(start, limit, condition, column, orderBy);
	}

	@Override
	public int getCountByCondition(Registration condition) {
		return registrationDao.getCountByCondition(condition);
	}

	/**
	 * 查看是否已挂过号
	 */
	@Override
	public Registration getYesNo(Registration registration) {
		//判断现在为上午还是下午,0上午，1下午
		GregorianCalendar ca = new GregorianCalendar();
		Integer shangxiawu = ca.get(GregorianCalendar.AM_PM);
		//2.查询该病人有人已经挂号
		registration.setStage(shangxiawu);
		//获取当前日期并格式化
		LqFormatDateUtils lqFormatDateUtils = new LqFormatDateUtils();
		Date d = lqFormatDateUtils.getTodayDate();
		registration.setRegistrationTime(d);
		
		return registrationDao.getYesNo(registration);
	}

	/**
	 * 换号
	 */
	@Override
	public String changeSigns(Registration registration) {
		//判断现在为上午还是下午,0上午，1下午
		GregorianCalendar ca = new GregorianCalendar();
		Integer shangxiawu = ca.get(GregorianCalendar.AM_PM);
		
		LqFormatDateUtils lqFormatDateUtils = new LqFormatDateUtils();
		Date d = lqFormatDateUtils.getTodayDate();
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		//1.查到原本信息
		Registration yuan = registrationDao.getById(registration.getId());
		//2.修改新换的医生限号，和原本医生限号
		DoctorRankClass yuanRankClass = doctorRankClassDao.getByRegistration(yuan);
		DoctorRankClass newRankClass = doctorRankClassDao.getByRegistration(registration);
		//判断是不是已经登记了。
		if(yuan.getRegisterState()==1) {
			return "该病人已经就诊登记了，无法换号！";
		}
		//判断有没有修改挂号信息
		if(yuan.getUser().getId()==registration.getUser().getId() && yuan.getType()==registration.getType() && yuan.getPaymentMethod().getId()==registration.getPaymentMethod().getId()) {
			return "请重新选择信息，你没有修改任何挂号信息！";
		}
			if(shangxiawu==0) {
				if(newRankClass.getAmRegistrationNumber()>=newRankClass.getAmLimitNumber()) {
					return "该医生上午挂号已满。";
				}
			}else {
				if(newRankClass.getPmRegistrationNumber()>=newRankClass.getPmLimitNumber()) {
					return "该医生下午挂号已满。";
				}
			}
			//修改排班医生挂号数量
		doctorRankClassDao.editLimitAndRegistration(newRankClass, shangxiawu);
		doctorRankClassDao.editAddLimitAndRegistration(yuanRankClass, shangxiawu);
		//3.修改挂号信息
		registrationDao.update(registration);
		System.out.println(registration);
		System.out.println(registration.getType());
		return "换号成功";
	}
	
	/**
	 * 退号
	 */
	@Override
	public String hmsRepulse(Integer id) {
		
		Registration registration =  registrationDao.getById(id);
		System.out.println(registration);
		if(registration.getRegisterState()==1) {
			return "该病人已登记过就诊了,无法删除！";
		}else {
			registrationDao.deleteById(id);
			return "退号成功";			
		}
		
	}

	@Override
	public Registration getById(Integer id) {
		return registrationDao.getById(id);
	}
 

}
